package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleDiseases;
import com.pubinfo.passbook.common.entity.TRuleDiseasesType;
import com.pubinfo.passbook.common.mapper.TRuleDiseasesMapper;
import com.pubinfo.passbook.common.mapper.TRuleDiseasesTypeMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleDiseasesParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TRuleDiseasesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 */
@Service
@Slf4j
public class TRuleDiseasesServiceImpl extends ServiceImpl<TRuleDiseasesMapper, TRuleDiseases>
    implements TRuleDiseasesService {
  
  @Resource
  private TRuleDiseasesTypeMapper typeMapper;
  
  @Override
  public IPage<TRuleDiseases> selectRuleDiseases(RuleDiseasesParam param) {
    IPage<TRuleDiseases> iPage = page(param.getPage("create_time"));
    return this.baseMapper.selectRuleDiseases(iPage, param);
  }
  
  @Override
  public JsonResult<String> saveRuleDiseases(TRuleDiseases diseases) {
    QueryWrapper<TRuleDiseases> wrapper = new QueryWrapper();
    wrapper.eq("name",diseases.getName());
    if (this.baseMapper.selectCount(wrapper) > 0)
        return JsonResult.error("病种名不能重复");
    TRuleDiseasesType tRuleDiseasesType =
        typeMapper.selectById(diseases.getDiseasesTypeId());
    if (tRuleDiseasesType == null)
        return JsonResult.error("病种类型不存在");
    this.baseMapper.insert(diseases);
    return JsonResult.success("新增成功");
  }
  
  @Override
  public JsonResult<String> updateRuleDiseases(TRuleDiseases diseases) {
    TRuleDiseasesType tRuleDiseasesType =
        typeMapper.selectById(diseases.getDiseasesTypeId());
    if (tRuleDiseasesType == null)
      return JsonResult.error("病种类型不存在");
    this.baseMapper.updateById(diseases);
    return JsonResult.success("更新成功");
  }
}




